package com.techshine.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import javax.annotation.Resource;


import com.techshine.util.AliOssUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.techshine.entity.Menu;
import com.techshine.service.MenuService;
import com.techshine.util.StringUtil;
import com.techshine.common.base.BaseController;
import com.techshine.common.base.BaseEntity;
import com.techshine.common.cache.FinishSpringContext;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Random;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.apache.commons.fileupload.FileItem;

/** 说明：菜单
 * 创建时间：2024-04-18
 */
@Controller
@RequestMapping(value="/menu")
public class MenuController extends BaseController {
	static String[] menu= {"首页","章程介绍","赛前培训","作品展示","联系我们"};	
	@Resource
	private MenuService menuService;
    @Resource
    private AliOssUtil aliOssUtil;
	@ResponseBody 
    @RequestMapping("/uploadpic")
    public Map<String, Object> file_upload(MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
//        String imgtype=(String)request.getParameter("imgtype");
//        String savePath="D:\\asdasda\\apache-tomcat-8.5.87\\apache-tomcat-8.5.87\\webapps\\techshine_war\\img\\";//资质，往期精彩，logo,轮播图
//        String saveUrl="./img/";
//        if("1".equals(imgtype)) {//设备图
//        	savePath="D:\\asdasda\\apache-tomcat-8.5.87\\apache-tomcat-8.5.87\\webapps\\techshine_war\\img\\equ\\";
//        	saveUrl="./img/equ/";
//        }else if("2".equals(imgtype) ) {//新闻通告图
//        	savePath="D:\\asdasda\\apache-tomcat-8.5.87\\apache-tomcat-8.5.87\\webapps\\techshine_war\\img\\news\\";
//        	saveUrl="./img/news/";
//        }else if("3".equals(imgtype) ) {//菜单主页图
//        	savePath="D:\\asdasda\\apache-tomcat-8.5.87\\apache-tomcat-8.5.87\\webapps\\techshine_war\\img\\main\\";
//        	saveUrl="./img/main/";
//        }else if("4".equals(imgtype) ) {//菜单主页图
//        	savePath="D:\\asdasda\\apache-tomcat-8.5.87\\apache-tomcat-8.5.87\\webapps\\techshine_war\\file\\";
//        	saveUrl="./file/";
//        }
//
//        map.put("errno", 1);
        List<String> list = new ArrayList<String>();
//        // 定义允许上传的文件扩展名
//        HashMap<String, String> extMap = new HashMap<String, String>();
//        extMap.put("image", "gif,jpg,jpeg,png,bmp");
//        // 最大文件大小
//        long maxSize = 50 * 1024 * 1024;//200k
//        if (!ServletFileUpload.isMultipartContent(request)) {
//            map.put("msg", "请选择文件。");
//            //writer.println(objectMapper.writeValueAsString(getError("请选择文件。")));
//            return map;
//        }
//        // 检查目录
//        File uploadDir = new File(savePath);
//        if (!uploadDir.exists()) {
//            uploadDir.mkdir();
//        }
//
//        if (!uploadDir.isDirectory()) {
//            map.put("msg", "上传目录不存在。");
//            return map;
//        }
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        String ymd = sdf.format(new Date());
//        savePath += ymd + "\\";
//        saveUrl += ymd + "/";
//
//        File dirFile = new File(savePath);
//        //判断文件夹是否存在
//
//        if (!dirFile.exists()) {
//            dirFile.mkdirs();
//        }
//        FileItemFactory factory = new DiskFileItemFactory();
//        ServletFileUpload upload = new ServletFileUpload(factory);
//
//
//        upload.setHeaderEncoding("UTF-8");
//        List items = upload.parseRequest(request);
//
//        DefaultMultipartHttpServletRequest mrequest = (DefaultMultipartHttpServletRequest) request;
//
//        Map map1 = mrequest.getFileMap();
//        Collection<MultipartFile> c = map1.values();
//        Iterator it = c.iterator();
//        for (; it.hasNext(); ) {
//            CommonsMultipartFile file = (CommonsMultipartFile) it.next();
//
//            if (!file.isEmpty()) {
//                long fileSize = file.getSize();
//                String fileName = file.getOriginalFilename();
//                try {
//                    File uploadedFile = new File(savePath, fileName);
//                    file.transferTo(uploadedFile);
//                } catch (Exception e) {
//                    map.put("msg", "上传文件失败。");
//                    map.put("errno", 1);
//
//                    return map;
//                }
//                map.put("saveurl", savePath+fileName);
//                map.put("name", fileName);
//                String imgUrl = saveUrl + fileName;
//                list.add(imgUrl);
//            }
//        }
//
//        map.put("errno", 0);
//        map.put("data", list);
//        return map;
        String path = aliOssUtil.upload(file.getOriginalFilename(), file.getInputStream());
        log.info("图片上传之后的访问路径是{}");
        System.out.println(path);
        map.put("saveurl", path);

                list.add(path);
        map.put("errno", 0);
        map.put("data", list);
        return map;
    }
	
	@ResponseBody 
    @RequestMapping("/queryByKey")
    public Map<String, Object>  queryByKey(String id) throws Exception {
    	Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtil.isNotEmpty(id)){
			Menu entity = menuService.queryByKey(id);
			map.put("result", entity);
			map.put("status", 200);
    	}else{
    		map.put("msg", "参数错误");
			map.put("status", 902);
    	}
		return map;
    }
   
    @ResponseBody 
    @RequestMapping("/queryListByPage")
    public Map<String, Object> queryListByPage(Menu entity) throws Exception {
    	Map<String, Object> map = new HashMap<String, Object>();
		List<Menu> list = menuService.queryListByPage(entity);
		map.put("code", 0); 
		map.put("data", list);
		map.put("count", entity.getCount());
		return map;
    }
    @ResponseBody 
    @RequestMapping("/queryList")
    public Map<String, Object> queryList(Menu entity) throws Exception {
    	Map<String, Object> map = new HashMap<String, Object>();
		
		List<BaseEntity> list = menuService.queryList(entity);
		map.put("status", 200);
		map.put("result", list);
		
		return map;
    }
    @ResponseBody 
    @RequestMapping("/insert")
    public Map<String, Object> insert(Menu entity) throws Exception {
    	Map<String, Object> map = new HashMap<String, Object>();
		//entity.setId(NumberUtil.randomUUID());
		//entity.setCreateTime(DateUtil.getCurrentDate());
		//entity.setUpdateTime(DateUtil.getCurrentDate());
    	entity.setCode(menu[entity.getParentid()]);
    	if(entity.getId()!=null&&entity.getId()>0) {
    		menuService.updateByKeySelective(entity);
    	}else {
    		menuService.insertSelective(entity);
    	}
		
		map.put("status", 200);
		map.put("msg", "成功!");
		
		return map;
    }
    @ResponseBody 
    @RequestMapping("/update")
    public Map<String, Object> update(Menu entity) throws Exception {
    	Map<String, Object> map = new HashMap<String, Object>();
		
		//entity.setUpdateTime(DateUtil.getCurrentDate());
		if(StringUtil.isNotEmpty(entity.getId())){
			menuService.updateByKeySelective(entity);
			map.put("result", entity);
			map.put("status", 200);
    	}else{
    		map.put("msg", "参数错误");
			map.put("status", 902);
    	}
		return map;
    }
    @ResponseBody 
    @RequestMapping("/delete")
    public Map<String, Object> delete(Menu entity) throws Exception {
    	Map<String, Object> map = new HashMap<String, Object>();
    	
		if(StringUtil.isNotEmpty(entity.getId())){
			menuService.delete(entity);
			map.put("result", entity);
			map.put("status", 200);
    	}else{
    		map.put("msg", "参数错误");
			map.put("status", 902);
    	}
		return map;
    }
    /**
     查询数据通过id**/
    @ResponseBody
    @RequestMapping("/byid")
    public Map<String, Object> byid(Menu entity) throws Exception {

        Map<String, Object> map = new HashMap<>();

        if (StringUtil.isNotEmpty(entity.getId())) {
            Menu entity1 = menuService.querybyid(entity);
            map.put("result", entity1);
            map.put("status", 200);
        } else {
            map.put("msg", "参数错误");
            map.put("status", 902);
        }

        return map;
    }
  
	
	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Map<String, Object> deleteAll() throws Exception{
		
		return null;
	}
	@RequestMapping(value="/delmore")
	@ResponseBody
	public Map<String, Object> delmore(String ids) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtil.isNotEmpty(ids)){
			menuService.delmore(ids);
			map.put("status", 200);
    	}else{
    		map.put("msg", "参数错误");
			map.put("status", 902);
    	}
		return map;
	}
}
